﻿/*****zxq*******/
    //插入话题
    function inTopic(id, str){
    var str = str || "请在这里输入自定义话题";
    str = "#" + str + "#";
    var reg = new RegExp(str);
   var oTextarea =document.getElementById(id);
   var oValue = oTextarea.value;
   var val_index = 0;
   var val_match;
   var val_pos;
    if(reg.test(oValue)){
    val_match = oValue.match(reg);
    val_index = val_match.index;
    }
    else{
    val_pos = getTextPos(oTextarea);
    val_index = val_pos.start;
    rangeText(oTextarea, str);
    }
    selectText(oTextarea, val_index + 1, val_index + str.length - 1);
    }
    //光标的当前位置
    function getTextPos(obj){
    if(!obj || obj.offsetWidth == 0){ return false; }
    var t = obj, start = 0, end = 0, value = '', range, range_all;
    value = t.value;
    if(typeof(t.selectionStart) == "number"){
    start = t.selectionStart;
    end = t.selectionEnd;
    }
    else if(document.selection && t.offsetWidth>0){
    t.focus();
    range = document.selection.createRange();
    if(range.parentElement() == t){
    range_all = document.body.createTextRange();
    range_all.moveToElementText(t);
    for(start = 0; range_all.compareEndPoints("StartToStart", range) < 0; start++){
    range_all.moveStart('character', 1);
    }
    for(var i = 0; i <= start; i++){
    if(t.value.charAt(i) == '\n'){
    start++;
    }
    }
    range_all.moveToElementText(t);
    for(end = 0; range_all.compareEndPoints('StartToEnd', range) < 0; end++){
    range_all.moveStart('character', 1);
    }
    for(var i = 0; i <= end; i++){
    if(t.value.charAt(i) == '\n'){
    end++;
    }
    }
    }
    }
    else{
    start = value.length;
    end = value.length;
    }
    return{
    start: start,
    end: end
    }
    }
    //光标处替换的文本
    function rangeText(obj, val, num){
    if(!obj || obj.offsetWidth == 0){ return false; }
    var t = obj, start = 0, end = 0, value = '', val_start = '', val_end = '', pos;
    var userAgent = navigator.userAgent.toLowerCase(), ie = /msie/.test(userAgent) && !/opera/.test(userAgent);
    if(ie && t.nodeName.toLocaleLowerCase() != 'txtText'){
    t.value = val;
    t.focus;
    return;
    }
    value = t.value;
    pos = getTextPos(t);
    start = pos.start;
    end = pos.end;
    if (typeof num=='number'){
    start = start - num;
    }
    val_start = value.substring(0, start);
    val_end = value.substring(end);
    t.value = val_start + val + val_end;
    if (typeof num=='number'){
    end = end - num;
    }
    selectText(t,end+val.length,end+val.length);
    }
    //光标定位到指定文本
    function selectText(obj, start, stop){
    if(!obj || obj.offsetWidth == 0){ return false; }
    var range;
    if(obj.setSelectionRange){
    obj.setSelectionRange(start,stop);
    }
    else{
    range = obj.createTextRange();
    range.collapse(true);
    range.moveStart("character",start);
    range.moveEnd("character",stop-start);
    range.select();
    }
    obj.focus();
    }